// ================================================== //
// ============= BEBOPTOOLS : BOPBOARD ============== //
// ================================================== //

// Samuel Kvaalen
// initialiser l'interface

// ================================================== //

// BEBOPDIR : acceder à la valeur de la variable d'environnement

// strcat modifie un par effet de bord (ici non)
char *concat(char *un, char *deux){
  int len_un, len_deux, i;
  char *out = 0;

  len_un = strlen(un); 
  len_deux = strlen(deux);
  
  out = (char *)malloc(sizeof(char) * (len_un + len_deux) + 1);
  for ( i = 0; i < len_un ; i++ ) {
    out[i] = un[i];
  }
  for ( i = 0; i < len_deux ; i++ ) {
    out[len_un + i] = deux[i];
  }
  out[len_un + len_deux] = '\0';
  return(out);
}

void init_bopboard_images_dir(){
  char *bebopdir = 0;
  char *reste = "/bopboard/images/";

  bebopdir = getenv("BEBOPDIR"); // contient deja un malloc
  if (bebopdir == 0) {
    // c'est que bebop n'est pas bien installé
    fprintf(stderr, "[!] Error : Env-Var \"BEBOPDIR\" Not Found\n");
    fprintf(stderr, "[!] Cannot load images.\nAborted\n");
    fflush(stderr);
    exit(1);
  }
  
  // strcat(a, b) ajoute b à la fin de a en modifiant a.   
  // bopboard_images_dir est alloué ( 1024 char dans var globales )
  bopboard_images_dir[0] = '\0';
  strcat(bopboard_images_dir, bebopdir);
  strcat(bopboard_images_dir, reste);
}

void init_images(){
	imgpush = (button_img)malloc(sizeof(one_button_img));
	imgswitch = (button_img)malloc(sizeof(one_button_img));
	imglight = (button_img)malloc(sizeof(one_button_img));

   
	imgpush->imgup = IMG_Load(concat(bopboard_images_dir, "pushup01.png"));
	imgpush->imgdown = IMG_Load(concat(bopboard_images_dir, "pushdown01.png"));
	imgswitch->imgup = IMG_Load(concat(bopboard_images_dir, "switchup02.png"));
	imgswitch->imgdown = IMG_Load(concat(bopboard_images_dir, "switchdown02.png"));
	imglight->imgup = IMG_Load(concat(bopboard_images_dir, "ladyoff01.png"));
	imglight->imgdown = IMG_Load(concat(bopboard_images_dir, "ladyon01.png"));
}

void init_plates(){
	int light_x = 10;
	int count = 0;
	plates[count].pos.x = 0+light_x;
	plates[count].pos.y = 5;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "ladybgleft01.png"));
	count++;
	
	int i;
	for(i=1;i<7;i++){
		plates[count].pos.x = 12 + i*100 +light_x;
		plates[count].pos.y = 5;
		plates[count].img = IMG_Load(concat(bopboard_images_dir, "ladybgmid01.png"));
		count++;
	}
	
	plates[count].pos.x = 12+100*7 +light_x;
	plates[count].pos.y = 5;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "ladybgright01.png"));
	count++;
	
	plates[count].pos.x = 0;
	plates[count].pos.y = WINHEIGHT - 70;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "pushbg01.png"));
	count++;

	plates[count].pos.x = 120 + (sizepush.x+20)*5;
	plates[count].pos.y = WINHEIGHT - 70;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "pushbg01.png"));
	count++;
	
	plates[count].pos.x = 120 + (sizepush.x+20)*6;
	plates[count].pos.y = WINHEIGHT - 70;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "pushbg01.png"));
	count++;

	plates[count].pos.x = 0;
	plates[count].pos.y = 125;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "switchbg02.png"));
	count++;

	plates[count].pos.x = 200 + 53*8;
	plates[count].pos.y = 125;
	plates[count].img = IMG_Load(concat(bopboard_images_dir, "switchbg02.png"));
	count++;
}

void init_buttons(){
	int i, count = 0;

	sizepush.x = 100; sizepush.y = 50;
	sizeswitch.x = 50; sizeswitch.y = 100;
	sizelight.x = 100; sizelight.y = 100;
	
	for(i=0;i<NUMPUSH;i++){
		push[i] = (gui_button)malloc(sizeof(one_button));
		push[i]->pos.x = 120 + (sizepush.x+20)*i;
		push[i]->pos.y = WINHEIGHT - 70;
		push[i]->size = sizepush;
		push[i]->pressed = 0;
		push[i]->img = &imgpush;
		buttons[count++] = &(push[i]);
	}
	for(i=0;i<NUMSWITCH;i++){
		sw[i] = (gui_button)malloc(sizeof(one_button));
		sw[i]->pos.x = 210 + 53*i;
		sw[i]->pos.y = 125;
		sw[i]->size = sizeswitch;
		sw[i]->pressed = 0;
		sw[i]->img = &imgswitch;
		buttons[count++] = &(sw[i]);
	}
	for(i=0;i<NUMLIGHT;i++){
		light[i] = (gui_button)malloc(sizeof(one_button));
		light[i]->pos.x = 20 + sizelight.x*i;
		light[i]->pos.y = 10;
		light[i]->size = sizelight;
		light[i]->pressed = 0;
		light[i]->img = &imglight;
		buttons[count++] = &(light[i]);
	}
}

void init_funcbuttons(){
	int i = 0;
	// func buttons
		funcbutton[i].pos.x = 230 + sizepush.x*5;
		funcbutton[i].pos.y = WINHEIGHT - 50;
		funcbutton[i].size.x = 69;
		funcbutton[i].size.y = 20;
		funcbutton[i].img = IMG_Load(concat(bopboard_images_dir, "relacher01.png"));
		funcbutton[i].func = *relacher;
		i++;

		funcbutton[i].pos.x = 75 + 230 + sizepush.x*5;
		funcbutton[i].pos.y = WINHEIGHT - 50;
		funcbutton[i].size.x = 33;
		funcbutton[i].size.y = 20;
		funcbutton[i].img = IMG_Load(concat(bopboard_images_dir, "fullscreen01.png"));
		funcbutton[i].func = *toggle_fullscreen;
		i++;
}

void free_images(){
	SDL_FreeSurface(imgpush->imgup);
	SDL_FreeSurface(imgpush->imgdown);	
	SDL_FreeSurface(imgswitch->imgup);
	SDL_FreeSurface(imgswitch->imgdown);
	SDL_FreeSurface(imglight->imgup);
	SDL_FreeSurface(imglight->imgdown);
	free(imgpush);
	free(imgswitch);
	free(imglight);
	int i;
	for(i=0;i<NUMBUTTONS;i++)
		free(*buttons[i]);
}
